package com.ruoyi.web.controller.iot;

import java.util.List;

import com.mongoplus.model.PageResult;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.mongo.domain.Record;
import com.ruoyi.common.mongo.service.IRecordService;
import com.ruoyi.common.mongo.domain.bo.RecordBo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.domain.IotAttendanceMachine;
import com.ruoyi.iot.service.IIotAttendanceMachineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤机Controller
 *
 * @author 陈江灿
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/iot/machine")
public class IotAttendanceMachineController extends BaseController {
    @Autowired
    private IIotAttendanceMachineService iotAttendanceMachineService;

    @Autowired
    private IRecordService recordService;

    /**
     * 识别记录
     * @return
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:recordList')")
    @GetMapping("/recordList")
    public AjaxResult recordList(RecordBo bo,Integer pageSize,Integer pageNum) {
        PageResult<Record> recordPageResult = recordService.queryPageList(bo, pageSize, pageNum);
        return success(recordPageResult);
    }

    /**
     * 查询考勤机列表
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotAttendanceMachine iotAttendanceMachine) {
        startPage();
        List<IotAttendanceMachine> list = iotAttendanceMachineService.selectIotAttendanceMachineList(iotAttendanceMachine);
        return getDataTable(list);
    }

    /**
     * 导出考勤机列表
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:export')")
    @Log(title = "考勤机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotAttendanceMachine iotAttendanceMachine) {
        List<IotAttendanceMachine> list = iotAttendanceMachineService.selectIotAttendanceMachineList(iotAttendanceMachine);
        ExcelUtil<IotAttendanceMachine> util = new ExcelUtil<IotAttendanceMachine>(IotAttendanceMachine.class);
        util.exportExcel(response, list, "考勤机数据");
    }

    /**
     * 获取考勤机详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:query')")
    @GetMapping(value = "/{attendanceMachineId}")
    public AjaxResult getInfo(@PathVariable("attendanceMachineId") Long attendanceMachineId) {
        return success(iotAttendanceMachineService.selectIotAttendanceMachineByAttendanceMachineId(attendanceMachineId));
    }

    /**
     * 新增考勤机
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:add')")
    @Log(title = "考勤机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotAttendanceMachine iotAttendanceMachine) {
        return toAjax(iotAttendanceMachineService.insertIotAttendanceMachine(iotAttendanceMachine));
    }

    /**
     * 修改考勤机
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:edit')")
    @Log(title = "考勤机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotAttendanceMachine iotAttendanceMachine) {
        return toAjax(iotAttendanceMachineService.updateIotAttendanceMachine(iotAttendanceMachine));
    }

    /**
     * 删除考勤机
     */
    @PreAuthorize("@ss.hasPermi('iot:machine:remove')")
    @Log(title = "考勤机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attendanceMachineIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceMachineIds) {
        return toAjax(iotAttendanceMachineService.deleteIotAttendanceMachineByAttendanceMachineIds(attendanceMachineIds));
    }
}
